package com.wenwu.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @project
 * @class ReflectBeanUtils.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月15日 上午12:10:30
 * @description 根据Java反射机制封装对象
 */
public class ReflectBeanUtils {
	/**
	 * @param data 要封装的数据，字符串格式name=value&name=value...
	 * @param clazz 要封装的实体类名
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws Exception
	 */
	public static Object getObject(String data, Class<?> clazz)
			throws IllegalAccessException, InvocationTargetException, Exception {
		HashMap<String, String> map = creatMap(data);
		if (map != null) {
			Object object = null;
			Method[] ms = null;
			Field[] fields = null; // 获取对象的所有属性
			String type = null;// 属性数据类型名
			String first = null;
			String methodName = null;
			String key = null;
			String val = null;
			Iterator<String> iter = null;
			try {
				object = clazz.newInstance();
				ms = clazz.getMethods();
				fields = clazz.getDeclaredFields();
				iter = map.keySet().iterator();
				while (iter.hasNext()) {
					key = iter.next();
					val = map.get(key);
					for (Field field : fields) {
						if (field.getName().equals(key)) {
							type = field.getGenericType().toString();// 获取当前属性的数据类型
							first = key.substring(0, 1);
							methodName = "set" + first.toUpperCase()
									+ key.split(first)[1];
							for (Method m : ms) {
								if (m.getName().equals(methodName)) {
									if (type.equals("class java.lang.Integer")
											|| type.equals("int")) {// Integer型
										m.invoke(object, Integer.valueOf(val));
										break;
									}
									if (type.equals("class java.lang.Double")
											|| type.equals("double")) {
										m.invoke(object, Double.valueOf(val));
										break;
									}
									if (type.equals("class java.lang.Boolean")
											|| type.equals("boolean")) {
										m.invoke(object, Boolean.valueOf(val));
										break;
									}
									if (type.equals("class java.lang.Short")
											|| type.equals("short")) {
										m.invoke(object, Short.valueOf(val));
										break;
									}
									if (type.equals("class java.util.Date")) {
										SimpleDateFormat sdf = new SimpleDateFormat(
												"yyyy-MM-dd HH:mm:ss");
										if (val.length() == 10) {
											val += " 00:00:00";
										}
										m.invoke(object, sdf.parse(val));
										break;
									}
									m.invoke(object, val);
									break;
								}
							}
							break;
						}
					}
				}
				return object;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {// return之后释放内存
				object = null;
				ms = null;
				fields = null;
				type = null;
				first = null;
				methodName = null;
				key = null;
				val = null;
				iter = null;
			}
		}
		return null;
	}

	// 将字符串封装成map对象
	public static HashMap<String, String> creatMap(String data) {
		HashMap<String, String> map = null;
		String[] datas = null;
		String[] strs = null;
		try {
			if (data != null && !data.equals("") && data.indexOf("&") >= 0) {
				datas = data.split("&");
				map = new HashMap<String, String>();
				for (String str : datas) {
					if (str != null && !str.equals("") && str.indexOf("=") >= 0) {
						strs = str.split("=");
						if (strs.length == 2) {
							map.put(strs[0], strs[1]);
						}
					}
				}
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {// 释放内存
			map = null;
			datas = null;
			strs = null;
		}
		return null;
	}
}